package com.cuit.qx.controller;

import com.cuit.qx.aop.OperLog;
import com.cuit.qx.bean.*;
import com.cuit.qx.dao.RoleGroupMapper;
import com.cuit.qx.dao.UserMapper;
import com.cuit.qx.service.MenuInfoService;
import com.cuit.qx.service.RoleNodeService;
import com.cuit.qx.utils.FileUtils;
import com.cuit.qx.utils.tools.StringUtils;

import net.sf.json.JSON;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin(origins = "*", maxAge = 3600)
@Controller
@RequestMapping(value="/api/MenuInfoAction")
public class MenuInfoAction {

	@Autowired
	private MenuInfoService menuInfoService;
	@Autowired
	private RoleNodeService roleNodeService;
	@Resource
	private RoleGroupMapper roleGroupMapper;
	
	static Logger logger = LogManager.getLogger(MenuInfoAction.class.getName());
	
	
	/**
	 * 查询主面板上的header菜单
	 * @param node
	 */
	@RequestMapping(value="/initHeaderMenuInfoList")
	@OperLog(operModul = "菜单管理", operType = "1", operDesc = "查询主面板上的header菜单")
	@ResponseBody
	public List<Menu> initHeaderMenuInfoList(HttpServletRequest request, String node){
		HttpSession session = request.getSession();
		User bean = (User) session.getAttribute("user");
		System.out.println(bean.getAccount());
		String userId = bean!=null?bean.getUserId():"2";
		List<Menu> list = this.menuInfoService.queryHeaderMenuInfoList(node, userId);
	    return list;
	}
	
	/**
	 * 查询主面板左边树菜单
	 * @param token
	 */
	@RequestMapping(value="/initLeftList")
	@ResponseBody
	public Map<String, Object> initLeftList(HttpServletRequest request, String token){
		Map<String,Object> resultMap=new HashMap<String,Object>();
		Map<String,Object> result=new HashMap<String,Object>();
		String role=token.substring(0,token.length()-6);
		RoleGroup oneRole = roleGroupMapper.getOneRole(role);
		if (oneRole==null){
			resultMap.put("role", role);
			resultMap.put("menuList", new ArrayList<>());
			result.put("code", 20000);
			result.put("data", resultMap);
			return result;
		}
		String groupId = oneRole.getGroupId();
		Menu menusBean = menuInfoService.getMenusLeftAll("1001",groupId);
		List<Menu> list= menusBean.getChildren();
		resultMap.put("role", role);
		resultMap.put("menuList", list);
		result.put("code", 20000);
		result.put("data", resultMap);
        return result;
	}
	
	/**
	 * 获取角色管理下权限设置 菜单资源树
	 * 
	 * @param
	 * @return
	 */
	@RequestMapping(value="/getMenusCheckForTree")
	@OperLog(operModul = "菜单管理", operType = "1", operDesc = "根据角色id获取角色管理下权限设置菜单资源树")
	@ResponseBody
	public Map<String, Object> getMenusCheckForTree(HttpServletRequest request, String roleId) {
		TreeInfoBean treeBean = this.menuInfoService.queryAllMenuByRoleId(roleId);
		List<TreeInfoBean> list = new ArrayList<TreeInfoBean>();
		list.add(treeBean);
		List<String> menuNameList = menuInfoService.getMenuNameList(roleId);
		RoleNode rold = roleNodeService.getById(roleId);
		Map<String, Object> result = new HashMap<String, Object>();
        result.put("code", 20000);
        result.put("data", list);
        result.put("authIds", menuNameList);
        if(rold!=null){
			String[] ids = rold.getHalfNode().split(",");
			result.put("bnode",ids);
		}
        return result;
	}
	
	/**
	 * 查询菜单管理页面左边面板的树
	 * 
	 * @param
	 * @return
	 */
	@RequestMapping(value="/getMenuPageLeftTree")
	@OperLog(operModul = "菜单管理", operType = "1", operDesc = "查询菜单管理页面左边面板的树")
	@ResponseBody
	public List<TreeInfoBean> getMenuPageLeftTree() {
		TreeInfoBean treeBean = this.menuInfoService.queryAllMenuByRoleId(null);
		List<TreeInfoBean> list = new ArrayList<TreeInfoBean>();
		list.add(treeBean);
		return list;
	}
	
	/**
	 * 查询菜单管理页面右侧数据
	 * 
	 * @return
	 */
	@RequestMapping(value="/queryMenuPageRight")
	@OperLog(operModul = "菜单管理", operType = "1", operDesc = "查询菜单管理页面右侧数据")
	@ResponseBody
	public PageList<Menu> queryMenuPageRight(String parentId, Integer page, Integer rows) {
		int start = page;
		int pageSize = rows;
		PageList<Menu> pageList = menuInfoService.queryMenuPageRight(parentId, start, pageSize);
		return pageList;
	}
	
	/**
	 * 得到菜单icon
	 * 
	 * @return
	 */
	@RequestMapping(value="/getMenuIcons")
	@OperLog(operModul = "菜单管理", operType = "1", operDesc = "得到菜单icon")
	@ResponseBody
	public List<String> getMenuIcons(HttpServletRequest request, String iconpath) {
		String path = request.getSession().getServletContext().getRealPath("/");
		if(!StringUtils.isNullOrEmpty(iconpath)){
			path += iconpath;
		}else{
			path += "\\resources\\electric_skin\\icons\\";
		}
		List<String> list = null;
		try {
			list = FileUtils.getImageNames(path);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}
	
	/**
	 * 添加菜单
	 */
	@RequestMapping(value="/addMenu")
	@OperLog(operModul = "菜单管理", operType = "2", operDesc = "添加菜单")
	@ResponseBody
	public Map<String, Object> addMenu(Menu menu) {
		int flag = menuInfoService.addMenu(menu);
		Map<String,Object> reulst = new HashMap<String, Object>();
		if (flag > 0)
		{
			reulst.put("success", true);
			return reulst;
		}
		reulst.put("success", false);
		return reulst;
	}
	
	
	/**
	 * 判断菜单是否存在
	 * 
	 * @return
	 */
	@RequestMapping(value="/checkNameIsExsit")
	@ResponseBody
	public Map<String, Object> checkNameIsExsit(String name) {
		Map<String,Object> reulst = new HashMap<String, Object>();
		if(StringUtils.isNullOrEmpty(name)){reulst.put("success", true);return reulst;} 
		int count = menuInfoService.checkNameIsExsit(name);
		if (count > 0)
		{
			reulst.put("success", false);
			return reulst;
		}
		reulst.put("success", true);
		return reulst;
	}
	
	/**
	 * 删除菜单
	 */
	@RequestMapping(value="/deleteMenu")
	@ResponseBody
	public Map<String, Object> deleteMenu(String id) {
		int falg = menuInfoService.deleteMenu(id);
		Map<String,Object> reulst = new HashMap<String, Object>();
		if (falg > 0){
			reulst.put("success", true);
			return reulst;
		}
		reulst.put("success", false);
		return reulst;
	}
	
	/**
	 * 
	 * 通过id查询菜单
	 */
	@RequestMapping(value="/queryMenuById")
	@ResponseBody
	public Menu queryMenuById(String id) {
		return menuInfoService.queryMenuById(id);
	}
	
	/**
	 * 修改菜单
	 */
	@RequestMapping(value="/updateMenu")
	@ResponseBody
	public Map<String, Object> updateMenu(Menu menu) {
		int flag = menuInfoService.updateMenu(menu);
		Map<String,Object> reulst = new HashMap<String, Object>();
		if (flag > 0){
			reulst.put("success", true);
		    return reulst;
		}
		reulst.put("success", false);
	    return reulst;
	}
	
	/**
	 * 
	 * 改变菜单状态
	 */
	@RequestMapping(value="/changeMenuStatus")
	@ResponseBody
	public Map<String, Object> changeMenuStatus(String menuId,String status){
		int flag = menuInfoService.changeMenuStatus(menuId,status);
		Map<String,Object> reulst = new HashMap<String, Object>();
		if (flag > 0){
			reulst.put("success", true);
		    return reulst;
		}
		reulst.put("success", false);
	    return reulst;
	}
	
	
	/**
	 * 查询所有菜单
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/getAllMenu")
	@ResponseBody
	public List<Menu> getAllMenu() {
		List<Menu> menus = this.menuInfoService.getAllMenu();
		return menus;
	}
	
}
